﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VIETPHONG
{
    public partial class frmSanpham : Form
    {
        private VIETPHONGEntities db = new VIETPHONGEntities();
        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public frmSanpham()
        {
            InitializeComponent();
        }

        private void frmSanpham_Load(object sender, EventArgs e)
        {
            dgvsanpham.AutoGenerateColumns = false;
            this.CenterToScreen();
            hienthisanpham();
            hienthicombobox();
        }
        private void hienthicombobox()
        {
            var query = from loaisp in db.LOAISANPHAMs
                        select loaisp;
            cboloaisp.DataSource = query.ToList();
            cboloaisp.DisplayMember = "TENLOAI";
            cboloaisp.ValueMember = "MALOAI";

            var query1 = from loaibaobi in db.LOAIBAOs
                         select loaibaobi;
            cboloaibb.DataSource = query1.ToList();
            cboloaibb.DisplayMember = "TENLOAIBAO";
            cboloaibb.ValueMember = "MALOAIBAO";
        }
        private void hienthisanpham()
        {
            var query = from sanpham in db.SANPHAMs
                        join loaisp in db.LOAISANPHAMs
                        on sanpham.MALOAI equals loaisp.MALOAI
                        join loaibao in db.LOAIBAOs
                        on sanpham.MALOAIBAO equals loaibao.MALOAIBAO
                        orderby sanpham.NGAYNHAP descending
                        select new
                        {
                            sanpham.MASANPHAM,
                            sanpham.TENSANPHAM,
                            sanpham.DONVITINH,
                            sanpham.DONGIA,
                            sanpham.MALOAI,
                            sanpham.MALOAIBAO,
                            loaisp.TENLOAI,
                            loaibao.TENLOAIBAO
                        };
            dgvsanpham.DataSource = query.ToList();
        }
        private void clear()
        {
            txtmasp.Text = "";
            txttensp.Text = "";
            txtdvt.Text = "";
            txtdongia.Text = "";            
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtmasp.Text) & String.IsNullOrEmpty(txttensp.Text) & String.IsNullOrEmpty(txtdvt.Text) & String.IsNullOrEmpty(txtdongia.Text))
            {
                MessageBox.Show("Thông tin cần nhập đầy đủ", "Thông báo");
            }
            else
            {
                try
                {
                    SANPHAM sanpham = new SANPHAM()
                    {
                        MASANPHAM = txtmasp.Text.ToUpper(),
                        TENSANPHAM = txttensp.Text,
                        DONVITINH = txtdvt.Text,
                        DONGIA = int.Parse(txtdongia.Text),
                        MALOAI = cboloaisp.SelectedValue.ToString(),
                        MALOAIBAO = cboloaibb.SelectedValue.ToString(),
                        NGAYNHAP = DateTime.Now
                    };
                    ChucNang.ThemSanPham(sanpham);
                    MessageBox.Show("Thêm thành công");
                    clear();
                }
                catch (Exception ex)
                {
                    var query = from sanpham in db.SANPHAMs
                                where sanpham.MASANPHAM.Equals(txtmasp.Text)
                                select sanpham;
                    if (query.ToList().Count >= 1)
                    {
                        MessageBox.Show("Trùng mã sản phẩm", "Thông báo");
                        txtmasp.Focus();
                    }
                    MessageBox.Show("Thêm không thành công", "Thông báo");
                }
            }
            hienthisanpham();   
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
             try
                {
                    SANPHAM sanpham = new SANPHAM()
                    {
                        MASANPHAM = txtmasp.Text.ToUpper(),
                        TENSANPHAM = txttensp.Text,
                        DONVITINH = txtdvt.Text,
                        DONGIA = int.Parse(txtdongia.Text),
                        MALOAI = cboloaisp.SelectedValue.ToString(),
                        MALOAIBAO = cboloaibb.SelectedValue.ToString(),
                        NGAYNHAP = DateTime.Now
                    };
                    ChucNang.CapNhatSanPham(sanpham);
                    MessageBox.Show("Cập nhật thành công !");
                }
            catch(Exception ex)
             {
                 MessageBox.Show("Cập Nhật thất bại !", "Warning");
             }
             hienthisanpham();
        }

        private void dgvsanpham_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dgvsanpham.Rows[e.RowIndex];
            txtmasp.Text = row.Cells[0].Value.ToString();
            txttensp.Text = row.Cells[1].Value.ToString();
            txtdvt.Text = row.Cells[2].Value.ToString();
            txtdongia.Text = row.Cells[3].Value.ToString();
            cboloaisp.Text = row.Cells[4].Value.ToString();
            cboloaibb.Text = row.Cells[5].Value.ToString();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {            
            try
            {
                SANPHAM sanpham = new SANPHAM()
                {
                    MASANPHAM = txtmasp.Text.ToUpper(),
                    TENSANPHAM = txttensp.Text,
                    DONVITINH = txtdvt.Text,
                    DONGIA = int.Parse(txtdongia.Text),
                    MALOAI = cboloaisp.SelectedValue.ToString(),
                    MALOAIBAO = cboloaibb.SelectedValue.ToString(),
                    NGAYNHAP = DateTime.Now
                };
                    ChucNang.XoaSanPham(sanpham);
                    MessageBox.Show("Xóa thành công !");                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Xóa thất bại !", "Warning");
            }
            hienthisanpham();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtmasp.Text = "";
            txtmasp.Focus();
            txttensp.Text = "";
            txtdongia.Text = "";
            txtdvt.Text = "";
        }        
    }
}
